package com.gx.smart.construction.ui.home.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseAdapter;
import com.gx.smart.base.BaseMVVMFragmentV2;
import com.gx.smart.common.util.AppConfig;
import com.gx.smart.construction.R;
import com.gx.smart.construction.databinding.HomeFragmentBinding;
import com.gx.smart.construction.ui.area.activity.AreaActivity;
import com.gx.smart.construction.ui.common.util.DataStoreHelper;
import com.gx.smart.construction.ui.elevator.activity.ElevatorControlActivity;
import com.gx.smart.construction.ui.home.adapter.FunctionAdapter;
import com.gx.smart.construction.ui.home.model.Function;
import com.gx.smart.construction.ui.home.viewmodel.HomeViewModel;
import com.gx.smart.construction.ui.house.activity.SmartHomeActivity;
import com.gx.smart.construction.ui.open.activity.OpenDoorActivity;
import com.gx.wisestone.constructionappgrpclib.grpc.systenant.ConstructionAppSysTenantDto;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.rxjava3.core.Flowable;
@AndroidEntryPoint
public class HomeFragment extends BaseMVVMFragmentV2<HomeViewModel> implements BaseAdapter.OnItemClickListener<Function> {

    @Inject
    FunctionAdapter adapter;
    private HomeFragmentBinding binding;
    private String area;

    public void switchArea(View v) {
        ActivityUtils.startActivity(new Intent(getActivity(), AreaActivity.class));
    }

    @Override
    public boolean getLeft() {
        return false;
    }

    @Override
    public int onBindLayout() {
        return R.layout.home_fragment;
    }

    @Override
    public String getToolbarTitle() {
        return "首页";
    }

    @Override
    protected Class<HomeViewModel> onBindViewModel() {
        return HomeViewModel.class;
    }


    @Override
    public void initContentView(ViewGroup root) {
        binding = DataBindingUtil.inflate(getLayoutInflater(), onBindLayout(), root, true);
        binding.setHomeFragment(this);
        binding.setLifecycleOwner(getViewLifecycleOwner());
        adapter.setItemClickListener(this);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        List<Function> functionList = new ArrayList<>();
        Function item1 = new Function();
        item1.setResId(R.mipmap.ic_smart_home);
        item1.setContent("智能家居");
        functionList.add(item1);

        Function item2 = new Function();
        item2.setResId(R.mipmap.ic_elevator_control);
        item2.setContent("梯控系统");
        functionList.add(item2);

        Function item3 = new Function();
        item3.setResId(R.mipmap.ic_open_door);
        item3.setContent("手机开门");
        functionList.add(item3);

        adapter.addAll(functionList);
    }

    private void displayArea() {
        DataStoreHelper dataStoreHelper = DataStoreHelper.getInstance();
        Flowable<String> flow = dataStoreHelper.readValue(dataStoreHelper.AREA_NAME_KEY);
        flow.onErrorReturn(throwable -> "").subscribe(s -> area = s);
        if (TextUtils.isEmpty(area)) {
            showInitLoadView(true);
            viewModel.getSysTenantList().observe(this, list -> {
                showInitLoadView(false);
                ConstructionAppSysTenantDto item = list.get(0);
                SPUtils.getInstance().put(AppConfig.SYS_TENANT_NO, item.getSysTenantNo());
                SPUtils.getInstance().put(AppConfig.AREA_NAME, item.getName());
                area = item.getName();
                binding.setAreaName(area);
            });
        } else {
            binding.setAreaName(area);
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        displayArea();
    }

    @Override
    public void onItemClick(Function function, int position) {
        if (TextUtils.isEmpty(area)) {
            ToastUtils.showLong("请先选择小区，再进行设置");
            return;
        }

        switch (position) {
            case 0:
                ActivityUtils.startActivity(new Intent(getActivity(), SmartHomeActivity.class));
                break;
            case 1:
                ActivityUtils.startActivity(new Intent(getActivity(), ElevatorControlActivity.class));
                break;
            case 2:
                ActivityUtils.startActivity(new Intent(getActivity(), OpenDoorActivity.class));
                break;
        }
    }

    @Override
    protected void initObserver() {
        viewModel.error.observe(this, result -> {
            if (result) {
                showInitLoadView(false);
            }
        });
    }
}